home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / 107_01.zip / FLOAT-44.C < prev    next >
C/C++ Source or Header  |  1993-06-06  |  4KB  |  163 lines

  1.  
  2.  
  3. /*
  4.     Floating point package support routines
  5.       modified two times by L. C. Calhoun see notes below
  6.  
  7.     Note the "fp" library function, available in DEFF2.CRL,
  8.     is used extensively by all the floating point number
  9.     crunching functions.
  10.  
  11.     (see FLOAT.DOC for details...)
  12.     (see FLOAT+44.DOC for details of revised version)
  13.  
  14.     NEW FEATURE: a special "printf" function has been included
  15.              in this source file for use with floating point
  16.              operands, in addition to the normal types. The
  17.              printf presented here will take precedence over
  18.              the DEFF.CRL version when "float" is specified
  19.              on the CLINK command line at linkage time.
  20.              Note that the "fp" function, needed by most of
  21.              the functions in this file, resides in DEFF2.CRL
  22.              and will be automatically collected by CLINK.
  23.  
  24.     All functions here written by Bob Mathias, except printf and
  25.     _spr (written by Leor Zolman.)
  26.  
  27.     New Functions Added    fpmag converts to floating magnitude
  28.                   fpchs changes sign of floating point no
  29.                   fpasg provides assignment of fl pt no
  30.                   ftoit converts fl pt no to trunc. int.
  31.                   ftoir converts fl pt no to rounded int.
  32.  
  33.                 written by L. C. Calhoun
  34.     Second Revision by L. C. Calhoun  
  35.                 Modify the _spr to use the z option
  36.                   as in STDLIB2
  37.                 Modify the program set to utilize the
  38.                   V 1.44 zero insert string variable
  39.  
  40. */
  41.  
  42. #include "bdscio.h"
  43.  
  44. #define NORM_CODE    0
  45. #define ADD_CODE    1
  46. #define SUB_CODE    2
  47. #define MULT_CODE    3
  48. #define DIV_CODE    4
  49. #define FTOA_CODE    5
  50. #define EXPON_SIGN    0x80   /* break point for exponent sign */
  51.  
  52. fpcomp(op1,op2)
  53.     char *op1,*op2;
  54. {
  55.     char work[5];
  56.     fpsub(work,op1,op2);
  57.     if (work[3] > 127) return (-1);
  58.     if (work[0]+work[1]+work[2]+work[3]) return (1);
  59.     return (0);
  60. }
  61.  
  62. fpnorm(op1) char *op1;
  63. {    fp(NORM_CODE,op1,op1);return(op1);}
  64.  
  65. fpadd(result,op1,op2)
  66.     char *result,*op1,*op2;
  67. {    fp(ADD_CODE,result,op1,op2);return(result);}
  68.  
  69. fpsub(result,op2,op1)
  70.     char *result,*op1,*op2;
  71.     {fp(SUB_CODE,result,op1,op2);return(result);}
  72.  
  73. fpmult(result,op1,op2)
  74.     char *result,*op1,*op2;
  75. {    fp(MULT_CODE,result,op1,op2);
  76.     return (result);
  77. }
  78.  
  79. fpdiv(result,op1,op2)
  80.     char *result,*op1,*op2;
  81. {    fp(DIV_CODE,result,op1,op2);return(result);}
  82.  
  83. atof(fpno,s)
  84.     char fpno[5],*s;
  85. {
  86.     char *fpnorm(),work[5],*ZERO,*FP_10;
  87.     int sign_boolean,power;
  88.  
  89.     FP_10 = "\0\0\0\120\4"; /* use as static variable */
  90.     ZERO = "\0\0\0\0\0";
  91.     setmem(fpno,5,0);
  92.     sign_boolean=power=0;
  93.  
  94.     while (*s==' ' || *s=='\t') ++s;
  95.     if (*s=='-'){sign_boolean=1;++s;}
  96.     for (;isdigit(*s);++s){
  97.         fpmult(fpno,fpno,FP_10);
  98.         work[0]=*s-'0';
  99.         work[1]=work[2]=work[3]=0;work[4]=31;
  100.         fpadd(fpno,fpno,fpnorm(work));
  101.     }
  102.     if (*s=='.'){
  103.         ++s;
  104.         for (;isdigit(*s);--power,++s){
  105.             fpmult(fpno,fpno,FP_10);
  106.             work[0]=*s-'0';
  107.             work[1]=work[2]=work[3]=0;work[4]=31;
  108.             fpadd(fpno,fpno,fpnorm(work));
  109.         }
  110.     }
  111.     if (toupper(*s) == 'E') {++s; power += atoi(s); }
  112.     if (power>0)
  113.         for (;power!=0;--power) fpmult(fpno,fpno,FP_10);
  114.     else
  115.     if (power<0)
  116.         for (;power!=0;++power) fpdiv(fpno,fpno,FP_10);
  117.     if (sign_boolean){
  118.         fpsub(fpno,ZERO,fpno);
  119.     }
  120.     return(fpno);
  121. }
  122. ftoa(result,op1)
  123.     char *result,*op1;
  124. {    fp(FTOA_CODE,result,op1);return(result);}
  125.  
  126. itof(op1,n)
  127. char *op1;
  128. int n;
  129. {
  130.     char temp[20];
  131.     return atof(op1, itoa(temp,n));
  132. }
  133.  
  134. itoa(str,n)
  135. char *str;
  136. {
  137.     char *sptr;
  138.     sptr = str;
  139.     if (n<0) { *sptr++ = '-'; n = -n; }
  140.     _uspr(&sptr, n, 10);
  141.     *sptr = '\0';
  142.     return str;
  143. }
  144.  
  145.  
  146. /*
  147.     The short "printf" function given here is exactly the
  148.     same as the one in the library, but it needs to be placed
  149.     here so that the special "_spr" is used instead of the
  150.     normal one in DEFF.CRL. The way the linker works is that
  151.     a function is not linked in UNTIL IT IS REFERENCED...so
  152.     if the definition of "printf" were not placed here in this
  153.     file, "_spr" would not be referenced at all
  154.     until the "printf" from DEFF.CRL got yanked in, at which time
  155.     "_spr" would ALSO be taken from DEFF.CRL and cause the
  156.     floating point "_spr" options to not be recognized.
  157.  
  158.     In other words, if "printf" were not given explicitly here,
  159.     the WRONG _spr would end up being used.
  160. */
  161.  
  162.  
  163. printf(